package com.example.mylogin.pojo.helper;

import lombok.Data;

import java.io.Serializable;

/**
 * <p>
 * 功能描述：返回结果封装
 * </p>
 *
 * @author cui haida
 * @date 2025/05/30/8:58
 */
@Data
public class R<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    private int code;
    private String msg;
    private T data;

    public static <T> R<T> ok() {
        return restResult(null, 200, "success");
    }

    public static <T> R<T> ok(String msg) {
        return restResult(null, 200, msg);
    }

    public static <T> R<T> ok(T data) {
        return restResult(data, 200, "success");
    }

    public static <T> R<T> ok(String msg, T data) {
        return restResult(data, 200, msg);
    }

    public static <T> R<T> error() {
        return restResult(null, 500, "error");
    }

    public static <T> R<T> error(String msg) {
        return restResult(null, 500, msg);
    }

    public static <T> R<T> error(int code, String msg) {
        return restResult(null, code, msg);
    }

    public R<T> data(T data) {
        this.data = data;
        return this;
    }

    private static <T> R<T> restResult(T data, int code, String msg) {
        R<T> apiResult = new R<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        return apiResult;
    }
}
